<template>
	<div class="user-page">
		<!--查询表单-->
		<el-card class="box-card mb10" shadow="never">
			<el-form ref="searchFormRef" class="search-form" :inline="true" label-position="left" label-width="75px" :model="searchForm">
				<el-row :gutter="30" justify="start">
					<el-col :span="6">
						<el-form-item class="form-item" label="应用名称:" prop="appId">
							<el-select v-model="searchForm.appId" class="el-form-item" clearable placeholder="请选择应用名称">
								<el-option v-for="item in appList" :label="item.name" :value="item.appId" :key="item.appId" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item class="form-item" label="所属模块:" prop="apiName">
							<dict-select v-model="searchForm.apiMod" :dictKey="'sys_operate_module'" placeholder="请选择所属模块" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item class="form-item" label="接口名称:" prop="apiName">
							<el-input v-model="searchForm.apiName" placeholder="请输入接口名称" />
						</el-form-item>
					</el-col>

					<template v-if="!searchMore">
						<el-col :span="6">
							<el-form-item class="form-item" label="接口状态:" prop="apiStatus">
								<el-select v-model="searchForm.apiStatus" class="el-form-item" clearable placeholder="请选择接口状态">
									<el-option label="启用" value="1" />
									<el-option label="停用" value="2" />
								</el-select>
							</el-form-item>
						</el-col>

						<el-col :span="6">
							<el-form-item class="form-item" label="接口地址:" prop="apiUrl">
								<el-input v-model="searchForm.apiUrl" placeholder="请输入接口地址" />
							</el-form-item>
						</el-col>
					</template>
					<el-col :span="6">
						<div class="search-btn">
							<el-button type="primary" @click="handleSearch">查询</el-button>
							<el-button @click="handleReset">重置</el-button>
							<a class="more-search" @click="searchMore = searchMore ? false : true">
								{{ searchMore ? '展开' : '收起' }}
								<el-icon>
									<component :is="searchMore ? 'ele-ArrowDown' : 'ele-ArrowUp'" color="#0052CC" />
								</el-icon>
							</a>
						</div>
					</el-col>
				</el-row>
			</el-form>
		</el-card>
		<el-card class="box-card" shadow="never">
			<static-table :tableData="tableData" :loading="loading" :seqConfig="{ startIndex: (pageNum - 1) * pageSize }">
				<template #vux-search>
					<el-row :gutter="12" class="mb10">
						<el-button class="ml8" type="primary" v-has="'system:auth:api:add'" @click="handleAdd">
							<el-icon><ele-Plus /></el-icon>
							&nbsp;新增
						</el-button>
					</el-row>
				</template>
				<template #vux-column>
					<vxe-column type="seq" title="序号" width="60" />
					<vxe-column field="apiName" min-width="120" show-overflow="tooltip" title="接口名称" />
					<vxe-column field="apiUrl" min-width="120" show-overflow="tooltip" title="接口地址" />
					<vxe-column field="apiPerms" min-width="150" show-overflow="tooltip" title="权限字符" />
					<vxe-column field="apiMod" min-width="120" :formatter="formatterModlue" show-overflow="tooltip" title="所属模块" />
					<vxe-column field="apiStatus" min-width="100" title="接口状态">
						<template #default="{ row }">
							<span style="margin-right: 10px">{{ row.apiStatus == '1' ? '正常' : '停用' }}</span>
							<el-switch v-has="'system:auth:api:update'" active-color="#00B42A" inactive-color="#F04141" :model-value="row.apiStatus == '1' ? true : false" @change="handleSwitch(row)" />
						</template>
					</vxe-column>
					<vxe-column field="orderNum" min-width="100" show-overflow="tooltip" title="排序" />
					<vxe-column field="createTime" min-width="150" show-overflow="tooltip" title="创建时间" />
					<vxe-column field="operation" fixed="right" min-width="80" title="操作">
						<template #default="{ row }">
							<div class="vxe-action">
								<a @click="handleUpdate(row)" v-has="'system:auth:api:update'">修改</a>
							</div>
						</template>
					</vxe-column>
				</template>
			</static-table>
			<el-pagination background :current-page="pageNum" :layout="layout" :page-size="pageSize" :total="dataCount" @current-change="handleCurrentChange" @size-change="handleSizeChange" />
		</el-card>
		<EditApiModal ref="editModalRef" :appList="appList" @peak-modal-ok="handleModalOk" />
	</div>
</template>
<script name="ApiManage" setup>
import global from '@/utils/global'
import useTableList from '@/hooks/useTableList'
import useModal from '@/hooks/useModal'
const EditApiModal = defineAsyncComponent(() => import('./leafPages/EditApiModal.vue'))

/*
 *@Description: 状态初始化
 *@MethodAuthor: liuYuFeng
 *@Date: 2023-08-28 10:11:35
 */

const state = reactive({
	searchForm: {
		appId: null,
		apiName: '',
		apiUrl: '',
		apiMod: '',
		apiStatus: '',
	},
	resetPwdModalRef: null,
	searchFormRef: null,
	appList: [],
})
const { searchForm, searchFormRef, appList } = toRefs(state)

const queryUrl = '/meta-system/web/v1/authApiInfo/list'
const { loading, dataCount, searchMore, handleInitQuery, getDictKeyText, handleModalOk, tableData, pageNum, pageSize, layout, handleSizeChange, handleCurrentChange } = useTableList(queryUrl, state.searchForm)

const { editModalRef, handleAdd, handleUpdate } = useModal()

/*
 *@Description: 生命周期
 *@MethodAuthor: liuYuFeng
 *@Date: 2023-09-15 10:56:51
 */

onMounted(() => {
	getAppList()
})

/*
 *@Description: 方法模块
 *@MethodAuthor: liuYuFeng
 *@Date: 2023-08-28 10:16:03
 */
const getAppList = () => {
	global
		.$fetchGetData(`/meta-system/web/v1/authApplication/applicationNameList`, {
			appType: 1,
		})
		.then(data => {
			if (data.code == 200) {
				state.appList = data.data
			} else {
				global.$baseMessage(data.msg, 'error')
			}
		})
}
const handleSearch = () => {
	handleInitQuery()
}
const handleReset = () => {
	state.searchFormRef.resetFields()
	handleInitQuery()
}
//switch 切换
const handleSwitch = row => {
	global.$baseConfirm(`是否确认更改名称为"--【${row.apiName}】--"的状态?`, '提示', () => {
		const user = {}
		user.businessId = row.id
		user.status = row.apiStatus == 1 ? 2 : 1
		global.$fetchPostData(`/meta-system/web/v1/authApiInfo/changeStatus`, user).then(data => {
			if (data.code == 200) {
				global.$baseMessage(data.msg, 'success')
				handleModalOk(2)
			} else {
				global.$baseMessage(data.msg, 'error')
			}
		})
	})
}
const formatterModlue = ({ cellValue }) => {
	return getDictKeyText('sys_operate_module', cellValue)
}
</script>
<style lang="scss" scoped></style>
